<template>
  <LoginDialog v-model="showLoginDialog" />
  <router-view :key="routerKey"></router-view>
</template>


<script setup lang="ts">
import { useRoute } from 'vue-router'
import { computed, ref, onMounted, onUnmounted } from 'vue'
import LoginDialog from '@/components/LoginDialog.vue'
import { ElMessage } from 'element-plus'

const route = useRoute()
const routerKey = computed(() => route.path)
const showLoginDialog = ref(false)

// 全局事件监听器，用于显示登录对话框
const handleShowLoginDialog = () => {
  showLoginDialog.value = true
}

onMounted(() => {
  // 添加全局事件监听器
  window.addEventListener('showLoginDialog', handleShowLoginDialog)
  
  // 页面加载时检查URL参数，如果包含showLogin=true则显示登录对话框
  const urlParams = new URLSearchParams(window.location.search);
  if (urlParams.get('showLogin') === 'true') {
    showLoginDialog.value = true;
    ElMessage({
      message: '登录已过期，请重新登录',
      type: "warning",
      duration: 5 * 1000
    });
    // 清除URL参数
    window.history.replaceState({}, document.title, window.location.pathname);
  }
})

onUnmounted(() => {
  // 移除全局事件监听器
  window.removeEventListener('showLoginDialog', handleShowLoginDialog)
})
</script>


<style>
.app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
body {
  margin: 0;
}
</style>